import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:live_music/components/ui/theme.dart';
import 'package:live_music/models/user_info_model.dart';
import 'package:live_music/pages/other/click_see_other.dart';
import 'package:live_music/services/userService.dart';

class ListCardFriend extends StatefulWidget {
  ListCardFriend({Key key, this.index, this.userListItem, this.addFollowed})
      : super(key: key);
  final int index;
  final UserInfoModel userListItem;
  final Function addFollowed;
  @override
  _ListCardFriendState createState() => _ListCardFriendState();
}

class _ListCardFriendState extends State<ListCardFriend>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 12),
          margin: const EdgeInsets.only(top: 24),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CircleAvatar(
                radius: 25,
                backgroundImage: NetworkImage(widget.userListItem.avatarUrl),
              ),
              Expanded(
                child: Container(
                  padding: const EdgeInsets.only(bottom: 15),
                  margin: const EdgeInsets.only(left: 24),
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        width: .5,
                        color: WcaoTheme.placeholder.withOpacity(.5),
                      ),
                    ),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            widget.userListItem.name,
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                            ),
                          )
                        ],
                      ),
                      Container(
                        margin: const EdgeInsets.only(top: 4),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(
                              child: Container(
                                padding: const EdgeInsets.only(right: 24),
                                child: Text(
                                  widget.userListItem.description != ""
                                      ? widget.userListItem.description
                                      : "暂无简介",
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: WcaoTheme.secondary,
                                    fontSize: 12,
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),
              Container(
                  height: 30,
                  width: !widget.userListItem.hasFollowed ? 60 : 70,
                  margin: EdgeInsets.only(left: 10),
                  alignment: Alignment.center,
                  child: ElevatedButton(
                      style: ButtonStyle(
                          elevation: MaterialStateProperty.all(0),
                          shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20))),
                          backgroundColor: MaterialStateProperty.all(
                            !widget.userListItem.hasFollowed
                                ? Colors.redAccent
                                : Colors.white,
                          )),
                      onPressed: () {
                        onTapLikeButton(!widget.userListItem.hasFollowed);
                      },
                      child: Center(
                        child: Text(
                          !widget.userListItem.hasFollowed ? '关注' : "已关注",
                          style: TextStyle(
                              color: !widget.userListItem.hasFollowed
                                  ? Colors.white
                                  : Colors.grey,
                              fontSize: 12,
                              fontWeight: FontWeight.w500),
                        ),
                      )))
            ],
          ),
        ),
        onTap: () {
          Get.to(PageViewOther(userId: widget.userListItem.id));
        });
  }

  Future<bool> onTapLikeButton(bool isLiked) async {
    Map<String, dynamic> data = {
      "targetId": widget.userListItem.id,
      "targetType": 'USER',
    };
    if (isLiked) {
      await UserService.follow(data);
    } else {
      await UserService.cancelFollow(data);
    }
    widget.addFollowed(isLiked, widget.index);
    return isLiked;
  }
}
